<template>
	<view>
		<view class="address-list">
			<view class="a-address" v-for="(item,index) in addressList" :key="index">
				<!-- :class="item.default_currentAddress? 'active':''" -->
				<view class="left-text"  >
					<label class="radio" @click="updateCheck(item)">
						<radio :checked="item.default_currentAddress===1?true:false" />
					</label>
					<view class="name-tel" @tap="selectTap(item)">
						{{item.receive_name}}&nbsp;&nbsp; {{item.phone}}
					</view>
					<view class="address-box">
						{{item.province+ item.city+item.region}}
					</view>
				</view>
				<view class="right-edit" @tap="editAddess(item.id)"></view>
			</view>
		</view>
		<view class="no-address" @click="hanleToAddress">
			新增地址
		</view>
	</view>
</template>

<script>
	let _this
	export default {
		data() {
			return {
				// addressList: null
			}
		},
		onLoad() {
			_this = this
			this.getAddress()
		},
		onPullDownRefresh() {
			this.getAddress()
			// console.log(this.allCart)
		},
		computed: {
			addressList() {
				return this.$store.state.m_user.address
			}
		},
		onShow() {
			// this.getAddress()
			// this.init()
		},
		onUnload() {
			this.$store.commit('m_user/updateaddflag',false)
		},
		methods: {
			async init() {
				var t = this;
				t.$.getlocation().then(res => {
					console.log(res)
				}).catch(err => {
					console.log(err)
				})
			},
			updateCheck(item){
				this.$store.commit('m_user/updateDefault',item)
				this.$store.commit('m_user/updateCurrentAddress',item)
				console.log(item)
			},
			getAddress() {
				uni.$http.get('/user-server/api/address/v1/list').then(res => {
					// uni.$showMsg('添加地址成功')
					if (res.data.code === 200) {
						uni.stopPullDownRefresh();
						this.$store.commit('m_user/updateAddress', res.data.data)
						// _this.addressList = res.data.data
					}
				})
				// uni.request({
				// 	// url: 'http://192.168.1.111:8889/user-server/api/address/v1/list',
				// 	url:'http://yym2417581902.qicp.net/user-server/api/address/v1/list',
				// 	method: 'GET',
				// 	header: {
				// 		token: this.$store.state.m_user.token
				// 	},
				// 	success(res) {
				// 		if (res.data.code === 200) {
				// 			console.log(res)
				// 			// uni.$showMsg('添加地址成功')
				// 			_this.$store.commit('m_user/updateAddress', res.data.data)
				// 			// _this.addressList = res.data.data
				// 			uni.stopPullDownRefresh();
				// 		}
				// 	},
				// 	fail(res) {
				// 		// uni.$showMsg('请求失败')
				// 		uni.stopPullDownRefresh();
				// 	}
				// })
			},
			selectTap(item) {
				if(this.$store.state.m_user.addflag){
					this.$store.commit('m_user/updateCurrentAddress', item)
					uni.navigateTo({
						url:'../order/order'
					})
				}
				
				// console.log(item);  
			},
			editAddess(id) {
				console.log("edit item id:" + id);
			},
			addAddess() {
				console.log("tap add new Address");
			},
			hanleToAddress() {
				uni.navigateTo({
					url: '../new_address/new_address'
				})
			}
		}
	}
</script>

<style scoped>
	* {
		box-sizing: border-box;
	}

	.no-address {
		width: 90%;
		height: 100rpx;
		background-color: #ff557f;
		position: fixed;
		left: 50%;
		transform: translateX(-50%);
		bottom: 30px;
		font-size: 40rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 60rpx;
		color: #FFFFFF;
	}

	.no-address:hover {
		background-color: #da486f;
	}


	.container {
		background-color: #F2f2f2;
	}

	.address-list {
		width: 100%;
		background-color: #fff;
		/* margin-top: 20upx; */
		padding-bottom: 100upx;
	}

	.address-list .a-address {
		width: 720upx;
		margin-left: 30upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30upx 0;
		border-bottom: 1upx solid #eee;
	}

	.a-address .left-text {
		width: 610upx;
		box-sizing: border-box;
		padding-left: 58upx;
	}

	.a-address .left-text.active {

		background-size: 28upx auto;
	}

	.radio {
		position: absolute;
		left: 20rpx;
	}

	.a-address .left-text .name-tel {
		margin-bottom: 20upx;
	}

	.a-address .left-text .address-box {
		font-size: 24upx;
		color: #888888;
		line-height: 36upx;
	}

	.a-address .right-edit {
		width: 109upx;
		height: 100%;
		padding: 50upx 0 50upx 58upx;
		box-sizing: border-box;
		background: url() no-repeat 43upx center;
		background-size: 35upx auto;
	}

	.bottom-box {
		position: fixed;
		width: 100%;
		bottom: 0;
		font-size: 28upx;
		color: #000000;
		border-top: 1upx solid #eee;
		height: 100upx;
		line-height: 100upx;
		background: #fff url() no-repeat 705upx center;
		background-size: 15upx auto;
	}

	.bottom-box .add-btn {
		margin-left: 30upx;
		padding-left: 70upx;
		background: url() no-repeat left center;
		background-size: 40upx auto;
	}
</style>
